home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / src / GLTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-19  |  27.9 KB  |  1,095 lines

  1. /*
  2.  * $Id: GLTest.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $
  3.  *
  4.  * $Date: 2000/04/07 19:44:51 $
  5.  * $Revision: 1.1.1.1 $
  6.  *
  7.  * (C) 1999 by Hyperion
  8.  * All rights reserved
  9.  *
  10.  * This file is part of the MiniGL library project
  11.  * See the file Licence.txt for more details
  12.  *
  13.  */
  14.  
  15. #include "mgl/gl.h"
  16.  
  17. #include <math.h>
  18.  
  19. #ifdef __VBCC__
  20. #pragma amiga-align
  21. #endif
  22.  
  23. #include <exec/exec.h>
  24. #include <intuition/intuition.h>
  25. #include <graphics/gfx.h>
  26. #include <libraries/lowlevel.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>    //OF
  30. #include <dos/dos.h>
  31.  
  32. #ifdef __VBCC__
  33. #pragma default-align
  34. #endif
  35.  
  36. #ifdef __GNUC__
  37. #ifdef __PPC__
  38. #include <proto/exec.h>
  39. #include <proto/intuition.h>
  40. #include <proto/lowlevel.h>
  41. #include <proto/dos.h>
  42. #include <proto/graphics.h>
  43. #else
  44. #include <inline/exec.h>
  45. #include <inline/intuition.h>
  46. #include <inline/lowlevel.h>
  47. #include <inline/dos.h>
  48. #include <inline/graphics.h>
  49. #endif
  50. #endif
  51.  
  52. #ifdef __STORMC__
  53. //#include <string.h>    //OF (made include string.h for all compilers)
  54. #include <clib/powerpc_protos.h>
  55. #ifndef __PPC__
  56. #include <proto/exec.h>
  57. #include <proto/intuition.h>
  58. #include <proto/lowlevel.h>
  59. #include <proto/dos.h>
  60. #include <proto/graphics.h>
  61. #else
  62. #include <clib/exec_protos.h>
  63. #include <clib/intuition_protos.h>
  64. #include <clib/lowlevel_protos.h>
  65. #include <clib/dos_protos.h>
  66. #include <clib/graphics_protos.h>
  67. #endif
  68.  
  69.  
  70. #define M_PI 3.1415927
  71.  
  72.  
  73. int kprintf(char *format, ...)
  74. {
  75.     return 0;
  76. }
  77. #endif
  78.  
  79. #ifdef __VBCC__
  80. #pragma amiga-align
  81.  
  82. #include <extra.h>            //OF
  83. #include <proto/exec.h>
  84. #include <proto/intuition.h>
  85. #include <proto/lowlevel.h>
  86. #include <proto/dos.h>
  87. #include <proto/graphics.h>
  88.  
  89. #pragma default-align
  90.  
  91. #define   M_PI 3.14159265358979323846
  92.  
  93.     #ifndef inline
  94.     //#define inline        //OF (removed)
  95.     #endif
  96.  
  97. #endif
  98.  
  99. static char rcsid[] = "$Id: GLTest.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $";
  100. struct Library *LowLevelBase;
  101.  
  102. #ifndef __PPC__
  103. extern struct IntuitionBase *IntuitionBase;
  104. extern struct GfxBase *GfxBase;
  105. extern struct Library *UtilityBase;
  106. extern struct DosLibrary *DOSBase;
  107. extern struct ExecBase *SysBase;
  108. #endif
  109.  
  110. extern int kprintf(char *format, ...);
  111. #define DEBUG(x) kprintf x
  112.  
  113. extern void GLPrintMatrix(int);
  114.  
  115. static struct EClockVal eval;
  116. static float fps;
  117. static GLfloat fog_start, fog_end;
  118. static GLboolean fogon = GL_FALSE;
  119. static GLboolean sync = GL_TRUE;
  120.  
  121. typedef struct
  122. {
  123.     GLfloat x,y,z,u,v;
  124. } MyVertex;
  125.  
  126.  
  127. #ifndef __VBCC__
  128. static
  129. #endif
  130. MyVertex vertices1 [] =
  131. {
  132.     {-1, -1, -1, 0.5, 0.5}, // 0
  133.     {-1, -1,  1, 0.5, 0.0}, // 1
  134.     {-1,  1,  1, 0.0, 0.0}, // 2
  135.     {-1,  1, -1, 0.0, 0.5}, // 3
  136.     { 1,  1, -1, 0.0, 1.0}, // 4
  137.     { 1, -1, -1, 0.5, 1.0}, // 5
  138.     { 1, -1,  1, 1.0, 1.0}, // 6
  139.     {-1, -1,  1, 1.0, 0.5}  // 7
  140. };
  141.  
  142. #ifndef __VBCC__
  143. static
  144. #endif
  145. MyVertex vertices2 [] =
  146. {
  147.     { 1,  1,  1, 0.5, 0.5}, // 0
  148.     { 1, -1,  1, 0.5, 0.0}, // 1
  149.     { 1, -1, -1, 0.0, 0.0}, // 2
  150.     { 1,  1, -1, 0.0, 0.5}, // 3
  151.     {-1,  1, -1, 0.0, 1.0}, // 4
  152.     {-1,  1,  1, 0.5, 1.0}, // 5
  153.     {-1, -1,  1, 1.0, 1.0}, // 6
  154.     { 1, -1,  1, 1.0, 0.5}  // 7
  155. };
  156.  
  157. #ifndef __VBCC__
  158. static
  159. #endif
  160. MyVertex verticesS1 [] =
  161. {
  162.     {-1, -1, -1, 0.5, 0.5}, // 0
  163.     {-1, -1,  1, 0.5, 0.0}, // 1
  164.     {-1,  1, -1, 0.0, 0.5}, // 2
  165.     {-1,  1,  1, 0.0, 0.0}, // 3
  166.  
  167.     { 1,  1, -1, 0.0, 0.5}, // 4
  168.     { 1,  1,  1, 0.5, 0.5}, // 5
  169.     { 1, -1, -1, 0.0, 0.0}, // 6
  170.     { 1, -1,  1, 0.5, 0.0}, // 7
  171. };
  172.  
  173. #ifndef __VBCC__
  174. static
  175. #endif
  176. MyVertex verticesS2 [] =
  177. {
  178.     {-1,  1, -1, 0.0, 0.5}, // 0
  179.     { 1,  1, -1, 0.0, 1.0}, // 1
  180.     {-1, -1, -1, 0.5, 0.5}, // 2
  181.     { 1, -1, -1, 0.5, 1.0}, // 3
  182.  
  183.     {-1, -1,  1, 1.0, 0.5}, // 4
  184.     { 1, -1,  1, 1.0, 1.0}, // 5
  185.     {-1,  1,  1, 0.5, 1.0}, // 6
  186.     { 1,  1,  1, 0.5, 0.5}, // 7
  187. };
  188.  
  189.  
  190. struct ResInfo
  191. {
  192.     int width, height;
  193.     char *name;
  194. };
  195.  
  196. struct ResInfo Resolutions [] =
  197. {
  198.     {320, 240, "320 x 240"},
  199.     {400, 300, "400 x 300"},
  200.     {640, 480, "640 x 480"},
  201.     {800, 600, "800 x 600"}, // Out of memory...
  202.     {1024, 768, "1024 x 768"},
  203.     {-1, -1, NULL}
  204. };
  205.  
  206. GLint ResPtr;
  207. char *CurrentRes;
  208. GLboolean ShowRes = GL_TRUE;
  209. GLboolean zbuffer = GL_TRUE;
  210.  
  211. GLfloat mouse_x = 0.0, mouse_y = 0.0, mouse_z = 0.0;
  212. GLint offset = 0;
  213. GLfloat fov = 70.0;
  214. GLfloat inf_w = 0.1;
  215. GLfloat zback = 1000.0;
  216. GLfloat alpha = 1.0;
  217.  
  218.  
  219. GLubyte index_texture[] =
  220. {
  221.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  222.     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  223.     1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,
  224.     1,0,1,2,2,2,2,2,2,2,2,2,2,1,0,1,
  225.     1,0,1,2,1,1,1,1,1,1,1,1,2,1,0,1,
  226.     1,0,1,2,1,3,3,3,3,3,3,1,2,1,0,1,
  227.     1,0,1,2,1,3,1,1,1,1,3,1,2,1,0,1,
  228.     1,0,1,2,1,3,1,4,4,1,3,1,2,1,0,1,
  229.     1,0,1,2,1,3,1,4,4,1,3,1,2,1,0,1,
  230.     1,0,1,2,1,3,1,1,1,1,3,1,2,1,0,1,
  231.     1,0,1,2,1,3,3,3,3,3,3,1,2,1,0,1,
  232.     1,0,1,2,1,1,1,1,1,1,1,1,2,1,0,1,
  233.     1,0,1,2,2,2,2,2,2,2,2,2,2,1,0,1,
  234.     1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,
  235.     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  236.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  237. };
  238.  
  239. GLubyte palette[] =
  240. {
  241.     0x00, 0x00, 0x00,
  242.     0xFF, 0x00, 0x00,
  243.     0x00, 0xFF, 0x00,
  244.     0x00, 0x00, 0xff,
  245.     0xFF, 0xFF, 0xFF,
  246. };
  247.  
  248. static void ReplaceTexture(void)
  249. {
  250.     GLenum error;
  251.     int i=1;
  252.  
  253.     glDeleteTextures(1, (const unsigned int *)&i);
  254.     glBindTexture(GL_TEXTURE_2D, 1);
  255.  
  256.     glColorTable(GL_COLOR_TABLE, GL_RGB, 5, GL_RGB, GL_UNSIGNED_BYTE, palette);
  257.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, index_texture);
  258.  
  259.     error = glGetError();
  260.     kprintf("Error = %d\n", error);
  261. }
  262.  
  263. static void MakeRot(GLfloat angle1, GLfloat angle2)
  264. {
  265.     GLfloat sinel = (GLfloat)sin((double)angle1/180*M_PI);
  266.     GLfloat cosel = (GLfloat)cos((double)angle1/180*M_PI);
  267.     GLfloat sinaz = (GLfloat)sin((double)angle2/180*M_PI);
  268.     GLfloat cosaz = (GLfloat)cos((double)angle2/180*M_PI);
  269.  
  270.     GLfloat mat[16];
  271.  
  272.     mat[0] = cosaz;        mat[4] = 0.0;    mat[8] = -sinaz;       mat[12] = 0;
  273.     mat[1] = -sinel*sinaz; mat[5] = cosel;  mat[9] = -sinel*cosaz; mat[13] = 0.0;
  274.     mat[2] = cosel*sinaz;  mat[6] = sinel;  mat[10] = cosel*cosaz; mat[14] = 0.0;
  275.     mat[3] =               mat[7] =         mat[11] = 0.0;         mat[15] = 1.0;
  276.     glMultMatrixf(mat);
  277. }
  278.  
  279. static void UpRes(void)
  280. {
  281.     ResPtr++; if (Resolutions[ResPtr].width == -1) ResPtr = 0;
  282.     mglResizeContext(Resolutions[ResPtr].width, Resolutions[ResPtr].height);
  283.     CurrentRes = Resolutions[ResPtr].name;
  284.     ShowRes = GL_TRUE;
  285. }
  286.  
  287. void PrExit(void)
  288. {
  289.     if (LowLevelBase)   CloseLibrary(LowLevelBase);
  290.     exit(0L);
  291. }
  292.  
  293. void PrInit(void)
  294. {
  295.     LowLevelBase  = OpenLibrary("lowlevel.library", 40L);
  296.     if (!LowLevelBase) PrExit();
  297. }
  298.  
  299. GLdouble angle = 0.0;
  300. GLfloat mouse_angle_x = 0.0;
  301. GLfloat mouse_angle_y = 0.0;
  302. GLfloat tlow = 0.0;
  303. GLfloat offx = 0.f, offy = 0.f;
  304. GLdouble zclear = 1.0;
  305. GLenum primitive = GL_POLYGON;
  306.  
  307. /*
  308. ** Load a PPM file into memory.
  309. ** The resulting pointer can be free()'d
  310. */
  311. GLubyte *LoadPPM(char *name, GLint *w, GLint *h)
  312. {
  313.     int i;
  314.     unsigned long x,y;
  315.     FILE *f;
  316.     GLubyte *where;
  317.     static  char buffer2[256];
  318.  
  319.     f = fopen(name, "r");
  320.  
  321.     if (!f)
  322.     {
  323.         *w = 0; *h=0;
  324.         return NULL;
  325.     }
  326.     #ifndef __STORM__
  327.     i = fscanf(f, "P6\n%lu %lu\n255\n",&x, &y);    //OF (%lu)
  328.     #else
  329.     i = fscanf(f, "P6\n%lu\n%lu\n255\n", &x, &y);    //OF (%lu)
  330.     #endif
  331.  
  332.     if (i!= 2)
  333.     {
  334.         printf("Error scanning PPM header\n");
  335.         fclose(f);
  336.         *w = 0; *h = 0;
  337.         return NULL;
  338.     }
  339.  
  340.     *w = x;
  341.     *h = y;
  342.  
  343.     where = malloc(x*y*3);
  344.     if (!where)
  345.     {
  346.         printf("Error out of Memory\n");
  347.         fclose(f);
  348.         *w = 0; *h = 0;
  349.         return NULL;
  350.     }
  351.  
  352.     i = fread(where, 1, x*y*3, f);
  353.     fclose(f);
  354.  
  355.     if (i != x*y*3)
  356.     {
  357.         printf("Error while reading file\n");
  358.         free(where);
  359.         *w = 0; *h = 0;
  360.         return NULL;
  361.     }
  362.  
  363.     return where;
  364. }
  365.  
  366. void TexInit(void)
  367. {
  368.     GLubyte *tmap;
  369.     GLint x,y;
  370.  
  371.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  372.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  373.  
  374.     tmap = LoadPPM("data/t1.ppm",&x, &y);
  375.     if(!tmap)
  376.     printf("Error: tmap t1 is NULL\n");
  377.  
  378.     glBindTexture(GL_TEXTURE_2D, 1);
  379.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  380.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  381.     free(tmap);
  382.  
  383.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  384.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  385.  
  386.     tmap = LoadPPM("data/t2.ppm",&x, &y);
  387.     if(!tmap)
  388.     printf("Error: tmap t2 is NULL\n");
  389.  
  390.     glBindTexture(GL_TEXTURE_2D, 2);
  391.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  392.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  393.     free(tmap);
  394.  
  395.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  396.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  397.  
  398.     tmap = LoadPPM("data/stars.ppm",&x, &y);
  399.     if(!tmap)
  400.     printf("Error: tmap stars is NULL\n");glBindTexture(GL_TEXTURE_2D, 3);
  401.  
  402.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  403.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  404.     free(tmap);
  405.  
  406.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  407.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  408.  
  409. }
  410.  
  411. static void drawCubeFan(GLint tex1, GLint tex2)
  412. {
  413.     GLfloat w = 1.0;
  414.  
  415.     if (tex1 == tex2) w = inf_w;
  416.  
  417.     glBindTexture(GL_TEXTURE_2D, tex1);
  418.     glBegin(GL_TRIANGLE_FAN);
  419.         //glColor3f(1.0, 0.0, 0.0);
  420.         glTexCoord2f(vertices1[0].u, vertices1[0].v);
  421.         glVertex4f(vertices1[0].x, vertices1[0].y, vertices1[0].z,w);
  422.         if (tex1 != tex2) glColor4f(0.7, 0.7, 0.7, alpha);
  423.         glTexCoord2f(vertices1[1].u, vertices1[1].v);
  424.         glVertex4f(vertices1[1].x, vertices1[1].y, vertices1[1].z,w);
  425.         glTexCoord2f(vertices1[2].u, vertices1[2].v);
  426.         glVertex4f(vertices1[2].x, vertices1[2].y, vertices1[2].z,w);
  427.         glTexCoord2f(vertices1[3].u, vertices1[3].v);
  428.         glVertex4f(vertices1[3].x, vertices1[3].y, vertices1[3].z,w);
  429.         glTexCoord2f(vertices1[4].u, vertices1[4].v);
  430.         glVertex4f(vertices1[4].x, vertices1[4].y, vertices1[4].z,w);
  431.         glTexCoord2f(vertices1[5].u, vertices1[5].v);
  432.         glVertex4f(vertices1[5].x, vertices1[5].y, vertices1[5].z,w);
  433.         glTexCoord2f(vertices1[6].u, vertices1[6].v);
  434.         glVertex4f(vertices1[6].x, vertices1[6].y, vertices1[6].z,w);
  435.         glTexCoord2f(vertices1[7].u, vertices1[7].v);
  436.         glVertex4f(vertices1[7].x, vertices1[7].y, vertices1[7].z,w);
  437.     glEnd();
  438.  
  439.     glBindTexture(GL_TEXTURE_2D, tex2);
  440.     glBegin(GL_TRIANGLE_FAN);
  441.         if (tex1 != tex2) glColor4f(0.0, 1.0, 0.0, alpha);
  442.         glTexCoord2f(vertices2[0].u, vertices2[0].v);
  443.         glVertex4f(vertices2[0].x, vertices2[0].y, vertices2[0].z,w);
  444.         if (tex1 != tex2) glColor4f(0.7, 0.7, 0.7, alpha);
  445.         glTexCoord2f(vertices2[1].u, vertices2[1].v);
  446.         glVertex4f(vertices2[1].x, vertices2[1].y, vertices2[1].z,w);
  447.         glTexCoord2f(vertices2[2].u, vertices2[2].v);
  448.         glVertex4f(vertices2[2].x, vertices2[2].y, vertices2[2].z,w);
  449.         glTexCoord2f(vertices2[3].u, vertices2[3].v);
  450.         glVertex4f(vertices2[3].x, vertices2[3].y, vertices2[3].z,w);
  451.         glTexCoord2f(vertices2[4].u, vertices2[4].v);
  452.         glVertex4f(vertices2[4].x, vertices2[4].y, vertices2[4].z,w);
  453.         glTexCoord2f(vertices2[5].u, vertices2[5].v);
  454.         glVertex4f(vertices2[5].x, vertices2[5].y, vertices2[5].z,w);
  455.         glTexCoord2f(vertices2[6].u, vertices2[6].v);
  456.         glVertex4f(vertices2[6].x, vertices2[6].y, vertices2[6].z,w);
  457.         glTexCoord2f(vertices2[7].u, vertices2[7].v);
  458.         glVertex4f(vertices2[7].x, vertices2[7].y, vertices2[7].z,w);
  459.     glEnd();
  460. }
  461.  
  462. static void drawCubeStrip(GLint tex1, GLint tex2)
  463. {
  464.     GLfloat w = 1.0;
  465.  
  466.     if (tex1 == tex2) w = inf_w;
  467.  
  468.     glBindTexture(GL_TEXTURE_2D, tex1);
  469.     glBegin(GL_TRIANGLE_STRIP);
  470.        //glColor3f(1.0, 0.0, 0.0);
  471.         glTexCoord2f(verticesS1[0].u, verticesS1[0].v);
  472.         glVertex4f(verticesS1[0].x, verticesS1[0].y, verticesS1[0].z,w);
  473.          if (tex1 != tex2) glColor4f(0.7, 0.7, 0.7, alpha);
  474.         glTexCoord2f(verticesS1[1].u, verticesS1[1].v);
  475.         glVertex4f(verticesS1[1].x, verticesS1[1].y, verticesS1[1].z,w);
  476.         glTexCoord2f(verticesS1[2].u, verticesS1[2].v);
  477.         glVertex4f(verticesS1[2].x, verticesS1[2].y, verticesS1[2].z,w);
  478.         glTexCoord2f(verticesS1[3].u, verticesS1[3].v);
  479.         glVertex4f(verticesS1[3].x, verticesS1[3].y, verticesS1[3].z,w);
  480.         glTexCoord2f(verticesS1[4].u, verticesS1[4].v);
  481.         glVertex4f(verticesS1[4].x, verticesS1[4].y, verticesS1[4].z,w);
  482.         glTexCoord2f(verticesS1[5].u, verticesS1[5].v);
  483.         glVertex4f(verticesS1[5].x, verticesS1[5].y, verticesS1[5].z,w);
  484.         glTexCoord2f(verticesS1[6].u, verticesS1[6].v);
  485.         glVertex4f(verticesS1[6].x, verticesS1[6].y, verticesS1[6].z,w);
  486.         glTexCoord2f(verticesS1[7].u, verticesS1[7].v);
  487.         glVertex4f(verticesS1[7].x, verticesS1[7].y, verticesS1[7].z,w);
  488.     glEnd();
  489.  
  490.     glBindTexture(GL_TEXTURE_2D, tex2);
  491.     glBegin(GL_TRIANGLE_STRIP);
  492.         if (tex1 != tex2) glColor4f(0.0, 1.0, 0.0, alpha);
  493.         glTexCoord2f(verticesS2[0].u, verticesS2[0].v);
  494.         glVertex4f(verticesS2[0].x, verticesS2[0].y, verticesS2[0].z,w);
  495.         if (tex1 != tex2) glColor4f(0.7, 0.7, 0.7, alpha);
  496.         glTexCoord2f(verticesS2[1].u, verticesS2[1].v);
  497.         glVertex4f(verticesS2[1].x, verticesS2[1].y, verticesS2[1].z,w);
  498.         glTexCoord2f(verticesS2[2].u, verticesS2[2].v);
  499.         glVertex4f(verticesS2[2].x, verticesS2[2].y, verticesS2[2].z,w);
  500.         glTexCoord2f(verticesS2[3].u, verticesS2[3].v);
  501.         glVertex4f(verticesS2[3].x, verticesS2[3].y, verticesS2[3].z,w);
  502.         glTexCoord2f(verticesS2[4].u, verticesS2[4].v);
  503.         glVertex4f(verticesS2[4].x, verticesS2[4].y, verticesS2[4].z,w);
  504.         glTexCoord2f(verticesS2[5].u, verticesS2[5].v);
  505.         glVertex4f(verticesS2[5].x, verticesS2[5].y, verticesS2[5].z,w);
  506.         glTexCoord2f(verticesS2[6].u, verticesS2[6].v);
  507.         glVertex4f(verticesS2[6].x, verticesS2[6].y, verticesS2[6].z,w);
  508.         glTexCoord2f(verticesS2[7].u, verticesS2[7].v);
  509.         glVertex4f(verticesS2[7].x, verticesS2[7].y, verticesS2[7].z,w);
  510.     glEnd();
  511. }
  512.  
  513. #if !defined (__STORM__) && !defined (__VBCC__)
  514. static
  515. #endif
  516. inline void myVertex(int i)
  517. {
  518.     glTexCoord2f(vertices1[i].u, vertices1[i].v);
  519.     glVertex3f(vertices1[i].x, vertices1[i].y, vertices1[i].z);
  520. }
  521.  
  522. #if !defined (__STORM__) && !defined (__VBCC__)
  523.  
  524. static
  525. #endif
  526. inline void myVertex2(int i)
  527. {
  528.     glTexCoord2f(vertices2[i].u, vertices2[i].v);
  529.     glVertex3f(vertices2[i].x, vertices1[i].y, vertices2[i].z);
  530. }
  531.  
  532. static void drawCubeQuad(GLint tex1, GLint tex2)
  533. {
  534.     glDisable(GL_TEXTURE_2D);
  535.     glBegin(GL_QUADS);
  536.         glColor3f(1.0, 0.0, 0.0);
  537.         glVertex3f(-1,-1,-1); glColor3f(0.5, 0.5, 0.5);
  538.         glVertex3f(-1,1,-1); glVertex3f(1,1,-1); glVertex3f(1,-1,-1);
  539.  
  540.         glColor3f(1.0, 0.5, 0.0);
  541.         glVertex3f(-1,1,-1); glColor3f(0.5, 0.5, 0.5);
  542.         glVertex3f(-1,1,1); glVertex3f(1,1,1); glVertex3f(1,1,-1);
  543.  
  544.         glColor3f(0.0, 1.0, 0.0);
  545.         glVertex3f(1,1,-1); glColor3f(0.5, 0.5, 0.5);
  546.         glVertex3f(1,1,1); glVertex3f(1,-1,1); glVertex3f(1,-1,-1);
  547.  
  548.         glColor3f(0.0, 0.0, 1.0);
  549.         glVertex3f(1,1,1); glColor3f(0.5, 0.5, 0.5);
  550.         glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glVertex3f(1,-1,1);
  551.  
  552.         glColor3f(0.0, 0.0, 0.0);
  553.         glVertex3f(-1,1,1); glColor3f(0.5, 0.5, 0.5);
  554.         glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(-1,-1,1);
  555.  
  556.         glColor3f(1.0, 1.0, 1.0);
  557.         glVertex3f(-1,-1,1); glColor3f(0.5, 0.5, 0.5);
  558.         glVertex3f(-1,-1,-1);
  559.         glVertex3f(1,-1,-1);
  560.         glVertex3f(1,-1,1);
  561.  
  562.     glEnd();
  563.     glEnable(GL_TEXTURE_2D);
  564. }
  565.  
  566. static void drawCubePoly(GLint tex1, GLint tex2)
  567. {
  568.     glDisable(GL_TEXTURE_2D);
  569.     glBegin(GL_POLYGON);
  570.         glColor3f(1.0, 0.0, 0.0);
  571.         glVertex3f(-1,-1,-1); glColor3f(0.5, 0.5, 0.5);
  572.         glVertex3f(-1,1,-1); glVertex3f(1,1,-1); glVertex3f(1,-1,-1);
  573.     glEnd();
  574.     glBegin(GL_POLYGON);
  575.         glColor3f(1.0, 0.5, 0.0);
  576.         glVertex3f(-1,1,-1); glColor3f(0.5, 0.5, 0.5);
  577.         glVertex3f(-1,1,1); glVertex3f(1,1,1); glVertex3f(1,1,-1);
  578.     glEnd();
  579.     glBegin(GL_POLYGON);
  580.         glColor3f(0.0, 1.0, 0.0);
  581.         glVertex3f(1,1,-1); glColor3f(0.5, 0.5, 0.5);
  582.         glVertex3f(1,1,1); glVertex3f(1,-1,1); glVertex3f(1,-1,-1);
  583.     glEnd();
  584.     glBegin(GL_POLYGON);
  585.         glColor3f(0.0, 0.0, 1.0);
  586.         glVertex3f(1,1,1); glColor3f(0.5, 0.5, 0.5);
  587.         glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glVertex3f(1,-1,1);
  588.     glEnd();
  589.     glBegin(GL_POLYGON);
  590.         glColor3f(0.0, 0.0, 0.0);
  591.         glVertex3f(-1,1,1); glColor3f(0.5, 0.5, 0.5);
  592.         glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(-1,-1,1);
  593.     glEnd();
  594.     glBegin(GL_POLYGON);
  595.         glColor3f(1.0, 1.0, 1.0);
  596.         glVertex3f(-1,-1,1); glColor3f(0.5, 0.5, 0.5);
  597.         glVertex3f(-1,-1,-1);
  598.         glVertex3f(1,-1,-1);
  599.         glVertex3f(1,-1,1);
  600.     glEnd();
  601.     glEnable(GL_TEXTURE_2D);
  602. }
  603.  
  604.  
  605. typedef void (*drawFunc)(GLint, GLint);
  606.  
  607. drawFunc funcs[] =
  608. {
  609.     drawCubeFan,
  610.     drawCubeStrip,
  611.     drawCubeQuad,
  612.     drawCubePoly,
  613.     NULL,
  614. };
  615.  
  616. static void (*drawCube)(GLint, GLint) = drawCubeFan;
  617.  
  618. void Rot1(void)
  619. {
  620.     glLoadIdentity();
  621.     glTranslatef(0.f, 0.f, -8.f);
  622.     glRotatef(angle, 0.f, 1.f, 1.f);
  623. }
  624.  
  625. void Rot2(void)
  626. {
  627.     glLoadIdentity();
  628.     glTranslatef(1.0, 3.0, -8.f);
  629.     glRotatef(-angle, 0.f, 1.f, 1.f);
  630.     glScalef(0.8, 2.0, 1.2);
  631. }
  632.  
  633. void Rot3(void)
  634. {
  635.     glLoadIdentity();
  636.     glTranslatef(-3.0, 2.0, -7.0);
  637.  
  638. //    glRotatef(-90, 1,0,0);
  639.     glRotatefEXTs(-1.f, 0.f, GLROT_100);
  640.  
  641. //    glRotatef(90, 0,0,1);
  642.     glRotatefEXTs(1.f, 0.f, GLROT_001);
  643.  
  644. //    glRotatef(angle, 1.0, 0.0, 0.0);
  645.     glRotatefEXT(angle, GLROT_100);
  646.  
  647. //    glRotatef(-angle, 0.0, 1.0, 0.0);
  648.     glRotatefEXT(-angle, GLROT_010);
  649.  
  650. //    glRotatef(angle/2.0, 0.0, 0.0, 1.0);
  651.     glRotatefEXT(angle/2.0, GLROT_001);
  652.  
  653.     glRotatef(-angle, 1.0, 1.0, 1.0);
  654. }
  655.  
  656. void Rot4(void)
  657. {
  658.     glLoadIdentity();
  659.     glTranslatef(3.0, 3.0, -10.0);
  660. //    glRotatef(2.0*angle, 0.0, 0.0, 1.0);
  661.     glRotatefEXT(2.0*angle, GLROT_001);
  662. }
  663.  
  664. void Rot5(void)
  665. {
  666.     glScalef(20.0, 20.0, 20.0);
  667. }
  668.  
  669.  
  670. static
  671. GLboolean draw(void)
  672. {
  673.     static int framecount = 0;
  674.     ULONG fracsecs;
  675.     static  char buffer[256];
  676.     struct Window *win = mglGetWindowHandle();
  677.  
  678.  
  679.     #ifndef NODRAW
  680.     if (GL_FALSE == mglLockDisplay())
  681.     {
  682.         printf("Unable to lock drawing area\n");
  683.         return GL_FALSE;
  684.     }
  685.  
  686.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  687.     #endif
  688.  
  689.  
  690.     if (alpha != 1.0)
  691.     {
  692.         glEnable(GL_BLEND);
  693.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  694.     }
  695.  
  696.     glEnable(GL_TEXTURE_2D);
  697.  
  698.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  699.     glLoadIdentity();
  700.     //MakeRot(mouse_angle_x, mouse_angle_y);
  701.     MakeRot(angle, angle);
  702.     Rot5();
  703.     glFrontFace(GL_CW);
  704.     glColor4f(1.f, 1.f, 1.f, 1.f);
  705.     drawCube(3,3);
  706.     glFrontFace(GL_CCW);
  707.  
  708.     if (alpha != 1.0) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  709.  
  710.     Rot1();
  711.     glColor4f(1.0, 1.0, 0.0, alpha);
  712.     drawCube(1,2);
  713.  
  714.     Rot2();
  715.     glColor4f(1.0, 0.0, 0.0, alpha);
  716.     drawCube(1,2);
  717.  
  718.     Rot3();
  719.     glColor4f(0.0, 0.0, 1.0, alpha);
  720.     drawCube(1,2);
  721.  
  722.     Rot4();
  723.     glColor4f(0.0, 1.0, 1.0, alpha);
  724.     drawCube(1,2);
  725.  
  726.     #ifndef NODRAW
  727.     mglUnlockDisplay();
  728.     #endif
  729.  
  730.     mglSwitchDisplay();
  731.     framecount++;
  732.  
  733.     if (fogon == GL_TRUE)
  734.     {
  735.         Move(win->RPort, 10, win->Height-5);
  736.         sprintf(buffer, "S: %6.3f E: %6.3f", fog_start, fog_end);
  737.         Text(win->RPort, buffer, strlen(buffer));
  738.     }
  739.  
  740.     fracsecs  = ElapsedTime(&eval);
  741.     fracsecs &= 0xFFFF;
  742.  
  743.     fps = 65536.0/(float)(fracsecs+1);
  744.     
  745.     Move(win->RPort, win->Width - 65, 14);
  746.     sprintf(buffer, "fps:%4.2f", fps);
  747.     Text(win->RPort, buffer, strlen(buffer));
  748.  
  749.     return GL_TRUE;
  750. //    return GL_FALSE;
  751. }
  752.  
  753. static GLboolean idle (void)
  754. {
  755.   return draw ();
  756. }
  757.  
  758. static void reshape(int width, int height, int offset, float fov)
  759. {
  760.     GLfloat fog_color[4] = {0.6, 0.3, 0.1, 1.0};
  761.     glFogfv(GL_FOG_COLOR, fog_color);
  762.     glFogf(GL_FOG_MODE, GL_LINEAR);
  763.     glFogf(GL_FOG_START, 1.5);
  764.     glFogf(GL_FOG_END,   fog_end);
  765.  
  766.  
  767.     glMatrixMode(GL_PROJECTION);
  768.     glLoadIdentity();
  769.     gluPerspective(fov, 1.3333333, 1.0, (GLdouble)zback);
  770.  
  771.     glMatrixMode(GL_MODELVIEW);
  772.     glViewport(offset,offset, (GLint)width-2*offset, (GLint)height-2*offset);
  773.     glClearColor(0.0f, 0.f, 0.f, 1.f);
  774.     glClearDepth(zclear);
  775.     glEnable(GL_TEXTURE_2D);
  776.     if (alpha == 1.0) glEnable(GL_CULL_FACE);
  777.     else              glDisable(GL_CULL_FACE);
  778.  
  779. }
  780.  
  781. void ClampMouse(struct Window *window, int x, int y, GLboolean left, GLboolean right)
  782. {
  783.     if (left == GL_FALSE && right == GL_FALSE)
  784.     {
  785.         mouse_x = (GLfloat)x/(GLfloat)(window->Width)*8.0;
  786.         mouse_y = (GLfloat)(window->Height - y)/(GLfloat)(window->Height)*8.0;
  787.         if (mouse_x > 8.0) mouse_x = 8.0;
  788.         if (mouse_y > 8.0) mouse_y = 8.0;
  789.         if (mouse_x < 0.0) mouse_x = 0.0;
  790.         if (mouse_y < 0.0) mouse_y = 0.0;
  791.         mouse_x -= 4.0;
  792.         mouse_y -= 4.0;
  793.         mouse_x *= 2.0;
  794.         mouse_y *= 2.0;
  795.     }
  796.     else
  797.     if (left == GL_TRUE && right == GL_FALSE)
  798.     {
  799.         mouse_z = (GLfloat)y/(GLfloat)(window->Height)*15.0;
  800.         if (mouse_z > 15.0) mouse_z = 15.0;
  801.         if (mouse_z < 0.0) mouse_z = 0.0;
  802.         mouse_z -= 5.0;
  803.     }
  804.     else
  805.     if (left == GL_FALSE && right == GL_TRUE)
  806.     {
  807.         mouse_angle_x = (GLfloat)x/(GLfloat)(window->Width)*360.0;
  808.         mouse_angle_y = (GLfloat)y/(GLfloat)(window->Height)*360.0;
  809.     }
  810. }
  811.  
  812.  
  813. GLenum LockMode = MGL_LOCK_SMART;
  814.  
  815. void IdleHandler(void)
  816. {
  817.     angle+=1.0;
  818.     idle();
  819. }
  820.  
  821. static int fixpointtrans = GL_NICEST; // T switches routine
  822. static GLenum shade = GL_SMOOTH;
  823.  
  824.  
  825. void KeyHandler(char key)
  826. {
  827.     struct Window *window;
  828.     static int drawfn = 0;
  829.     static GLenum WHint = GL_DONT_CARE;
  830.     window = (struct Window *)mglGetWindowHandle();
  831.  
  832.     switch(key)
  833.     {
  834.       case 'g':
  835.           if (shade == GL_SMOOTH)
  836.         {
  837.             glShadeModel(GL_FLAT);
  838.             shade = GL_FLAT;
  839.         }
  840.         else
  841.         {
  842.             glShadeModel(GL_SMOOTH);
  843.             shade = GL_SMOOTH;
  844.         }
  845.         break;
  846.         case '1':
  847.             if (WHint == GL_DONT_CARE) WHint = GL_FASTEST;
  848.             else                       WHint = GL_DONT_CARE;
  849.             glHint(MGL_W_ONE_HINT, WHint);
  850.             break;
  851.         case 's':
  852.             if (sync == GL_FALSE)   sync = GL_TRUE;
  853.             else                sync = GL_FALSE;
  854.             mglEnableSync(sync);
  855.             break;
  856.         case '8':
  857.             fog_end++; if (fog_end > zback) fog_end = zback;
  858.             glFogf(GL_FOG_END, fog_end);
  859.             break;
  860.         case '5':
  861.             fog_end--; if (fog_end < fog_start) fog_end = fog_start;
  862.             glFogf(GL_FOG_END, fog_end);
  863.             break;
  864.         case '7':
  865.             fog_start++; if (fog_start > fog_end) fog_start = fog_end;
  866.             glFogf(GL_FOG_START, fog_start);
  867.             break;
  868.         case '4':
  869.             fog_start--; if (fog_start < 1.5) fog_start = 1.5;
  870.             glFogf(GL_FOG_START, fog_start);
  871.             break;
  872.         case 'f':
  873.             if (fogon == GL_FALSE)
  874.             {
  875.                 glEnable(GL_FOG);
  876.                 fogon = GL_TRUE;
  877.             }
  878.             else
  879.             {
  880.                 glDisable(GL_FOG);
  881.                 fogon = GL_FALSE;
  882.             }
  883.             break;
  884.         case 'd':
  885.             drawfn++; if (funcs[drawfn] == NULL) drawfn = 0;
  886.             drawCube = funcs[drawfn];
  887.             break;
  888.         case 27:
  889.             mglExit();
  890.             break;
  891.         case '+':
  892.             if (zclear < 1.0) zclear += 0.01;
  893.             if (zclear > 1.0) zclear = 1.0;
  894.             glClearDepth(zclear);
  895.             break;
  896.         case '-':
  897.             if (zclear > 0.0) zclear -= 0.01;
  898.             if (zclear < 0.0) zclear = 0.0;
  899.             glClearDepth(zclear);
  900.             break;
  901.         case 'e':
  902.             mouse_z = 10.0;
  903.             break;
  904.         case 'w':
  905.             offset += 10;
  906.             if (offset >=100) offset = 100;
  907.             reshape((int)window->Width,(int)window->Height, offset, fov);
  908.             break;
  909.         case 'q':
  910.             offset -= 10;
  911.             if (offset <=0) offset = 0;
  912.             reshape((int)window->Width,(int)window->Height, offset, fov);
  913.         case 't':
  914.         glHint(MGL_FIXPOINTTRANS_HINT, fixpointtrans);
  915.         if(fixpointtrans == GL_FASTEST)
  916.             fixpointtrans = GL_NICEST;
  917.         else
  918.             fixpointtrans = GL_FASTEST;
  919.             break;
  920.         case 'y':
  921.             fov += 2.0;
  922.             if (fov>180.0) fov = 180.0;
  923.             reshape((int)window->Width,(int)window->Height, offset, fov);
  924.             break;
  925.         case 'x':
  926.             fov -= 2.0;
  927.             if (fov<40.0) fov = 40.0;
  928.             reshape((int)window->Width,(int)window->Height, offset, fov);
  929.             break;
  930.         case 'z':
  931.             if (zbuffer == GL_TRUE)
  932.             {
  933.                 zbuffer = GL_FALSE;
  934.                 glDisable(GL_DEPTH_TEST);
  935.             }
  936.             else
  937.             {
  938.                 zbuffer = GL_TRUE;
  939.                 glEnable(GL_DEPTH_TEST);
  940.             }
  941.             break;
  942.         case 'r':
  943.             ReplaceTexture();
  944.             break;
  945.     }
  946. }
  947.  
  948. void MouseHandler(GLint MouseX, GLint MouseY, GLbitfield buttons)
  949. {
  950. }
  951.  
  952. void MainLoop(void)
  953. {
  954.     struct Window *window;
  955.  
  956.  
  957.     window = (struct Window *)mglGetWindowHandle();
  958.  
  959.     TexInit();
  960.  
  961.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  962.  
  963.     reshape((int)window->Width,(int)window->Height, offset, fov);
  964.     ElapsedTime(&eval);
  965.     SetAPen(window->RPort, 2);
  966.     glEnable(GL_DEPTH_TEST);
  967.     glEnable(GL_TEXTURE_2D);
  968.  
  969.     mglLockMode(LockMode);
  970.     mglKeyFunc(KeyHandler);
  971.     mglMouseFunc(MouseHandler);
  972.     mglIdleFunc(IdleHandler);
  973.  
  974.     mglMainLoop();
  975. }
  976.  
  977. int main(int argc, char **argv)
  978. {
  979.     int i;
  980.     int numb=3, resnr=0;
  981.     GLint width=320; GLint height=240;
  982.  
  983.     printf("Calling MGLInit...\n");
  984.     PrInit();
  985.     MGLInit();
  986.  
  987.     for (i=1; i<argc; i++)
  988.     {
  989.         if (0 == strcmp(argv[i], "-window"))
  990.         {
  991.             mglChooseWindowMode(GL_TRUE);
  992.         }
  993.         else if (0 == strcmp(argv[i], "-zback"))
  994.         {
  995.             i++;
  996.             zback = (GLfloat)atof(argv[i]);
  997.         }
  998.         else if (0 == strcmp(argv[i], "-w"))
  999.         {
  1000.             i++;
  1001.             inf_w = (GLfloat)atof(argv[i]);
  1002.         }
  1003.         else if (0 == strcmp(argv[i], "-buffers"))
  1004.         {
  1005.             i++;
  1006.             numb = atoi(argv[i]);
  1007.         }
  1008.         else if (0 == strcmp(argv[i], "-res"))
  1009.         {
  1010.             i++;
  1011.             resnr = atoi(argv[i]);
  1012.         }
  1013.         else if (0 == strcmp(argv[i], "-alpha"))
  1014.         {
  1015.             i++;
  1016.             alpha = atof(argv[i]);
  1017.         }
  1018.         else if (0 == strcmp(argv[i], "-lock"))
  1019.         {
  1020.             i++;
  1021.             if (0 == stricmp(argv[i], "manual"))
  1022.             {
  1023.                 LockMode = MGL_LOCK_MANUAL;
  1024.             }
  1025.             else if (0 == stricmp(argv[i], "auto"))
  1026.             {
  1027.                 LockMode = MGL_LOCK_AUTOMATIC;
  1028.             }
  1029.             else if (0 == stricmp(argv[i], "smart"))
  1030.             {
  1031.                 LockMode = MGL_LOCK_SMART;
  1032.             }
  1033.             else printf("Unknown lockmode. Using default\n");
  1034.         }
  1035.         else
  1036.         {
  1037.             printf("Unknown option %s\n", argv[i]);
  1038.             printf("Usage: %s -zback <float> -w <float> -res <int> -lock (manual|auto|smart) -buffers <int>\n", argv[0]);
  1039.             exit(0);
  1040.         }
  1041.     }
  1042.  
  1043.     ResPtr = resnr;
  1044.     CurrentRes = Resolutions[ResPtr].name;
  1045.  
  1046.     fog_start = 1.5;
  1047.     fog_end = 100.0;
  1048.  
  1049.     //mglSetDebugLevel(10);
  1050.     printf("Setting number of buffers to %d...\n", numb);
  1051.     mglChooseNumberOfBuffers(numb);
  1052.     printf("Setting pixel depth to 16...\n");
  1053.     mglChoosePixelDepth(16);
  1054.     printf("Creating context...\n");
  1055.  
  1056.     width = Resolutions[ResPtr].width;
  1057.     height = Resolutions[ResPtr].height;
  1058.  
  1059.     if (mglCreateContext(0,0, Resolutions[ResPtr].width, Resolutions[ResPtr].height))
  1060.     {
  1061.         if (alpha != 1.0) glShadeModel(GL_FLAT);
  1062.         printf("Switching sync...\n");
  1063.         mglEnableSync(GL_TRUE);
  1064.  
  1065.         printf("Going into main loop...\n");
  1066.  
  1067.         MainLoop();
  1068.         printf("Done\n");
  1069.         mglDeleteContext();
  1070.     }
  1071. /*
  1072.     else (mglCreateContextFromID(0x50011102,&width,&height))
  1073.     {
  1074.         if (alpha != 1.0) glShadeModel(GL_FLAT);
  1075.         printf("Switching sync...\n");
  1076.         mglEnableSync(GL_TRUE);
  1077.         printf("Going into main loop...\n");
  1078.  
  1079.         MainLoop();
  1080.         printf("Done\n");
  1081.         mglDeleteContext();
  1082.     }
  1083. */
  1084.     else 
  1085.     {
  1086.         printf("Error: Can't mglCreateContext()\n");
  1087.     }
  1088.  
  1089.     MGLTerm();
  1090.     PrExit();
  1091.     return 0;
  1092. }
  1093.  
  1094.  
  1095.